import Api from '../../utils/api.js';
import prompt from '../../utils/prompt.js';
import {
  checkRequire,
  checkEarlier,
  checkLater,
  checkNumber,
  checkNumberBigger
} from '../../utils/validator.js';

const app = getApp();

// components/meetingForm/meetingForm.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    conferenceRoomList: [], // 会议室列表
    meetingCompositionList: [
      { key: '公司及以上领导', name: '公司及以上领导' },
      { key: '分公司领导', name: '分公司领导' },
      { key: '项目班子', name: '项目班子' },
      { key: '项目主管', name: '项目主管' },
      { key: '项目全体人员', name: '项目全体人员' },
      { key: '项目系统人员', name: '项目系统人员' }
    ], // 人员组成列表
    drinkList: ['热茶', '矿泉水', '暂不需要'], // 茶水列表
    roomConflicts: [], // 冲突时间列表

    others: '', // 其他
    LaserPen: false, // 是否需要激光笔
    multiMedia: true, // 是否需要多媒体
    drink: '', // 茶水配置
    meetingComposition: '', // 会议人员组成
    meetingCount: '', // 会议人数
    endDateTime: '', // 结束时间
    startDateTime: '', // 开始时间
    conferenceRoom: '', // 会议室

    ps_meetingComposition: true,
    ps_drink: true,
    ps_meetingCount: true,
    ps_endDateTime: true,
    ps_startDateTime: true,
    ps_conferenceRoom: true
  },

  attached () {
    this.getAllMeetingRoom();
    // 获得multiple组件  多选
    this.multiple = this.selectComponent('#multiple');
    this.meetingDialog = this.selectComponent('#meeting-dialog');
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /* --===================功能函数===================-- */
    // 查询会议室
    async getAllMeetingRoom () {
      const res = await Api.getMeetingRooms({
        sessionKey: app.globalData.sessionKey
      });
      if (res) {
        this.setData({
          conferenceRoomList: res.message.map(room => room.meetingRoom)
        });
      }
    },
    // 表单提价
    async submitMeeting () {
      const param = {
        'endTime': this.data.endDateTime,
        'laserPointer': this.data.LaserPen ? '1' : '0',
        'meetingMembers': this.data.meetingComposition,
        'meetingNum': +this.data.meetingCount,
        'meetingRoom': this.data.conferenceRoom,
        'multimedia': this.data.multiMedia ? '1' : '0',
        'remark': this.data.others,
        'startTime': this.data.startDateTime,
        'teaWaterOpt': this.data.drink
      };
      const res = await Api.applyMeeting(param, app.globalData.sessionKey);
      if (res) {
        if (res.retCode === '0') {
          prompt.showToast('会议室预约成功', 'success', { mask: true });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        } else if (res.retCode === '2025') {
          this.setData({ roomConflicts: res.message });
          this.meetingDialog.showModal({
            title: '当前会议室会议时间存在冲突'
          });
        } else {
          if (res.retMsg) {
            prompt.showModal(res.retMsg);
          }
        }
      }
    },

    /* --===================功能函数===================-- */
    // 输入检查
    checkInput () {
      let ps_conferenceRoom = false;
      let ps_startDateTime = false;
      let ps_endDateTime = false;
      let ps_meetingCount = false;
      let ps_drink = false;
      let ps_meetingComposition = false;

      ps_conferenceRoom = checkRequire(this.data.conferenceRoom, '请选择会议室');
      this.setData({ ps_conferenceRoom });

      if (ps_conferenceRoom) {
        const current = new Date();
        const sevenDays = 7 * 24 * 60 * 60 * 1000;
        ps_startDateTime = (
          checkRequire(this.data.startDateTime, '请选择开始时间') &&
          checkEarlier(new Date(this.data.startDateTime), new Date(current.getTime() + sevenDays), '请选择7日以内的时间') &&
          checkLater(new Date(this.data.startDateTime), current, '无效的会议时间')
        );
        this.setData({ ps_startDateTime });
      }

      if (ps_startDateTime) {
        ps_endDateTime = (
          checkRequire(this.data.endDateTime, '请选择结束时间') &&
          checkLater(new Date(this.data.endDateTime), new Date(this.data.startDateTime), '结束时间应晚于开始时间')
        );
        this.setData({ ps_endDateTime });
      }

      if (ps_endDateTime) {
        ps_meetingCount = (
          checkRequire(this.data.meetingCount, '请输入会议人数') &&
          checkNumber(this.data.meetingCount, '请输入正确的会议人数') &&
          checkNumberBigger(this.data.meetingCount, 2, '至少2人参与会议')
        );
        this.setData({ ps_meetingCount });
      }

      if (ps_meetingCount) {
        ps_drink = checkRequire(this.data.drink, '请选择茶水配置');
        this.setData({ ps_drink });
      }

      if (ps_drink) {
        ps_meetingComposition = checkRequire(this.data.drink, '请选择参会人员类型');
        this.setData({ ps_meetingComposition });
      }

      return (
        ps_conferenceRoom &&
        ps_startDateTime &&
        ps_endDateTime &&
        ps_meetingCount &&
        ps_drink &&
        ps_meetingComposition
      );
    },

    /* --===================事件处理===================-- */
    // 数输入事件
    handleInput (e) {
      const param = {};
      param[e.currentTarget.dataset.state] = e.detail.value;
      this.setData(param);
    },
    // 获取 picker input 组件值
    handlePickerInputValue (e) {
      const param = {};
      param[e.currentTarget.dataset.value] = e.detail.value;
      this.setData(param);
    },
    // 表单提交
    handleSubmit () {
      if (this.checkInput()) {
        this.submitMeeting();
      }
    },
    // 多选重置
    handleMultipleCancel () {
      this.multiple.hideMultiple();
    },
    // 多选确定
    handleMultipleConfirm (e) {
      const param = {};
      param[e.currentTarget.dataset.type] = e.detail.map(i => i.name).join('，');
      this.setData(param);
      this.multiple.hideMultiple();
    },
    // 多选入口
    handleSelectMultiple () {
      this.multiple.showMultiple();
    }
  }
});
